<script type="text/javascript">
	function SwiperInit(vm) {
		var x = 0;
		var pages = new Swiper('.TabSwiperPages', {
			noSwiping: true,
			onTouchMove: function(){
				vm.$store.commit('isSwipering',true);
				if(x == 0){
					x = setTimeout(function(){
						vm.$store.commit('isSwipering',false);
						x = 0;
					},300);
				}	
			},
			onSlideChangeStart: function() {
				if(vm.firstUpdate && vm.firstUpdate[pages.activeIndex]){
					vm.loadMore(pages.activeIndex);
					vm.firstUpdate[pages.activeIndex] = false;
				}
				if(vm && vm.activeIndex != undefined){
					vm.activeIndex = pages.activeIndex;
				}
				Dom7(".TabSwiperNav .active").removeClass('active')
				Dom7(".TabSwiperNav .swiper-slide").eq(pages.activeIndex).addClass('active');
			},
		});
		
		var nav = new Swiper('.TabSwiperNav', {
			freeMode: true,
			freeModeMomentumRatio: 1,
			slidesPerView: 'auto',
			shortSwipes: false,
			longSwipes: 0.3,
			onTap: function(nav,e) {
				pages.slideTo(nav.clickedIndex, 300, false);
				Dom7(".TabSwiperNav .active").removeClass('active');
				Dom7(".TabSwiperNav .swiper-slide").eq(pages.activeIndex).addClass('active');
				vm.activeIndex = nav.clickedIndex;
			},
		});		
		
		pages.params.control = nav;
		
		return { nav , pages };
	};

	function TimeInit(ele,vm,set = []) {
		var myCalendar = f7.calendar({
			input: Dom7(ele),
			monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
			dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
			value:set,
			disabled: function (date) {
				//最大时间为今天
				if (date > new Date()) {
					return true;
				}
				else {
					return false;
				}
			},
			onDayClick: function(p, dayContainer, year, month, day) {
				myCalendar.close();
			},
			onChange: function(p, values){
				//vm.$store.commit('getTime');
			}
		});
		return myCalendar;
	}

	function AutoCompleteInit(ele, vm, url, name, openIn) {
		return f7.autocomplete({
			openIn: 'dropdown', //open in page
			input: '#' + ele, //link that opens autocomplete
			/*backText: '<i class="iconfont icon-zuo"></i>',
			searchbarPlaceholderText: '请输入关键字',
			searchbarCancelText: '取消',*/
			notFoundText: '未找到相关信息',
			//backOnSelect: true, //go back after we select something
			valueProperty: name, //object's "value" property name
			textProperty: name, //object's "text" property name
			//preloader: true, //enable preloader
			limit: 100,//显示最大条数
			dropdownPlaceholderText: '请输入关键字',
			source: function(autocomplete, query, render) {
				var results = [],
					Name = 'Name',
					objName;
				var SearchList = JSON.parse(localStorage.getItem(ele));
				if(query.length === 0) {
					render(results);
					return;
				}

				function Verification(obj) {
					for(let l in obj) {
						if(obj[l].toString().indexOf(query.toString()) >= 0) {
							return true;
							break;
						}
					}
					return false;
				}

				if(url != null && url.length != 0) {
					switch(url) {
						case 'getGoodsName':
							//主动预警-商品名称
							vm.$ajax.getGoodsName(localStorage.getItem('token'), query).then(res => {
								if(res.responseCode == 1) {
									for(let key in res.dataList) {
										if(res.dataList[key][name].toLowerCase().indexOf(query.toLowerCase()) >= 0) {
											results.push(res.dataList[key]);
										}
									}
									render(results);
								}
							});
							break;
						case 'getTaskName':
							//综合查询-任务名称
							vm.$ajax.searchTaskDirectoryLs().then(res => {
								if(res.responseCode == 1) {
									for(let key in res.dataList) {
										if(res.dataList[key][name].toLowerCase().indexOf(query.toLowerCase()) >= 0) {
											results.push(res.dataList[key]);
										}
									}
									render(results);
								}
							});
							break;
						case 'getAreaUnits':
							//综合查询-定点单位
							vm.$ajax.searchObjectLs(query).then(res => {
								if(res.responseCode == 1) {
									for(let key in res.dataList) {
										if(res.dataList[key][name].toLowerCase().indexOf(query.toLowerCase()) >= 0) {
											results.push(res.dataList[key]);
										}
									}
									render(results);
								}
							});
							break;
					}
				} else {
					for(var key in SearchList[0]) {
						if(key.toString().toLowerCase().indexOf(Name.toLowerCase()) >= 0) {
							objName = key;
						}
					}
					// Find matched items
					for(var i = 0; i < SearchList.length; i++) {
						if(Verification(SearchList[i])) {
							results.push(SearchList[i][objName].toString());
						}
					}
					// Render items by passing array with result items
					render(results);
				}

			},
			onChange: function(autocomplete, value) {
				if(url != null && url.length != 0) {
					vm[ele] = value[name];
					switch(url) {
						case 'getGoodsName':
							vm.$store.commit('autoCompleteObj', {obj: value ,index:0});
							break;
						case 'getTaskName':
							vm.$store.commit('autoCompleteObj', {obj: value ,index:1});
							break;
						case 'getAreaUnits':
							vm.$store.commit('autoCompleteObj', {obj: value ,index:2});
							break;
					}
				}else{
					vm[ele] = value;
				}

			}

		});
	}

	export default {
		SwiperInit,
		TimeInit,
		AutoCompleteInit,
	}
</script>